//
//  LZFRootViewController.m
//  Love Denim
//
//  Created by 刘志锋 on 16/8/29.
//  Copyright © 2016年 longroader. All rights reserved.
//

#import "LZFRootViewController.h"
#import "LZFBrandViewController.h"
#import "LZFChannelViewController.h"
#import "LZFTitleButton.h"

@interface LZFRootViewController () <UIScrollViewDelegate>
/** scrollView */
@property (nonatomic, weak) UIScrollView *scrollView;
/** 标题按钮 */
//@property (nonatomic, weak) LZFTitleButton *titleButton;
/** 被选中的按钮 */
@property (nonatomic, weak) LZFTitleButton *selectedTitleButton;
/** 标题按钮底部指示器 */
@property (nonatomic, weak) UIView *indicatorView;
/** 标题按钮整体 */
@property (nonatomic, weak) UIView *titlesView;
@end

@implementation LZFRootViewController

#pragma mark - 初始化
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor grayColor];
    
    [self setupChildVC];
    
    [self setupScrollView];
    
    [self setupTitlesView];
    
    [self addChildVcView];
}
/** 添加子控制器 */
- (void)setupChildVC {
    LZFBrandViewController *brand = [[LZFBrandViewController alloc] init];
    [self addChildViewController:brand];
    
    LZFChannelViewController *channel = [[LZFChannelViewController alloc] init];
    [self addChildViewController:channel];
}

- (void)setupScrollView {
    
    // 不允许scrollView自动调整内边距
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // 添加品牌/频道scrollView
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.backgroundColor = LZFRandomColor;
    scrollView.frame = self.view.bounds;
    NSUInteger childVcCount = self.childViewControllers.count;
    scrollView.contentSize = CGSizeMake(scrollView.zf_width * childVcCount, 0);
    
    scrollView.pagingEnabled = YES;
    scrollView.scrollEnabled = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
}

- (void)setupTitlesView {
    // 标题栏
    UIView *titlesView = [[UIView alloc] init];
    titlesView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
    titlesView.frame = CGRectMake(0, 0, self.view.zf_width, 60);
    [self.view addSubview:titlesView];
    
    // 添加标题按钮
    NSArray *titles = @[@"品牌", @"频道"];
    NSUInteger count = titles.count;
    CGFloat titleButtonW = titlesView.zf_width / count / 2;
    CGFloat titleButtonH = titlesView.zf_height - 20;
    for (NSUInteger i = 0; i < 2; i++) {
        
        LZFTitleButton *titleButton = [LZFTitleButton buttonWithType:UIButtonTypeCustom];
        titleButton.tag = i;
        [titleButton addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [titlesView addSubview:titleButton];
        [titleButton setTitle:titles[i] forState:UIControlStateNormal];
        titleButton.frame = CGRectMake(titleButtonW * (i + 1), 20, titleButtonW, titleButtonH);
    }
    
    // 添加底部指示器
    LZFTitleButton *firstTitleButton = titlesView.subviews.firstObject;
    
    UIView *indicatorView = [[UIView alloc] init];
    indicatorView.backgroundColor = [firstTitleButton titleColorForState:UIControlStateSelected];
    indicatorView.zf_height = 2; // 指示器高度
    indicatorView.zf_y = titlesView.zf_height - indicatorView.zf_height; // 指示器y值
    [titlesView addSubview:indicatorView];
    self.indicatorView = indicatorView;
    
    // 立刻根据文字内容计算好label的宽度
    [firstTitleButton.titleLabel sizeToFit];
    indicatorView.zf_width = firstTitleButton.titleLabel.zf_width;
    indicatorView.zf_centerX = firstTitleButton.titleLabel.zf_centerX;
    
    // 设置默认选中第一个的标题按钮
    firstTitleButton.selected = YES;
    [self titleClick:firstTitleButton];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    
    // 设置状态栏文字颜色
    return UIStatusBarStyleLightContent;
}

#pragma mark - 监听点击
- (void)titleClick:(LZFTitleButton *)titleButton {
    // 控制按钮状态
    self.selectedTitleButton.selected = NO;
    titleButton.selected = YES;
    self.selectedTitleButton = titleButton;
    
    // 让底部指示器跟随被点击按钮
    [UIView animateWithDuration:0.2 animations:^{
        self.indicatorView.zf_x = self.selectedTitleButton.zf_x;
        self.indicatorView.zf_width = titleButton.zf_width;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.3 animations:^{
            self.indicatorView.zf_width = titleButton.titleLabel.zf_width;
            self.indicatorView.zf_centerX = titleButton.zf_centerX;
        }];
    }];
    
    // 设置UIscrollView滚动到被点击标题的对应位置
    CGPoint offset = self.scrollView.contentOffset;
    offset.x = titleButton.tag * self.scrollView.zf_width;
    [self.scrollView setContentOffset:offset animated:YES];
}

#pragma mark - 添加子控制器的view
- (void)addChildVcView {
    
    NSUInteger index = self.scrollView.contentOffset.x / self.scrollView.zf_width;
    UIViewController *childVc = self.childViewControllers[index];
    
    if ([childVc isViewLoaded]) return;
    
    childVc.view.frame = self.scrollView.bounds;
    [self.scrollView addSubview:childVc.view];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    
    // 在滚动动画完成后添加子控制器
    [self addChildVcView];
}

//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    // 模拟点击对应的标题按钮
//    NSUInteger index = scrollView.contentOffset.x / scrollView.zf_width;
//    LZFTitleButton *titleButton = self.titlesView.subviews[index];
//    [self titleClick:titleButton];
    // 在拖拽scrollView完成后添加子控制器
//    [self addChildVcView];
//}

@end
